﻿//*************************Get Width, Height of Screen*************************

function getScreenWidth() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

function getScreenHeight() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
}

//*************************End Get Width, Height of Screen*************************

//*************************Popup Modal*************************
//Get Rad Window
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz az well)
    return oWindow;
}

function OpenModalWindow(index, url, width, height, posX, posY) {
    if (windowManagerID == '')
        windowManagerID = 'ctl00_ctl00_uxWindowManager';
    var windowManager = $find(windowManagerID);
    windowManager = windowManager.get_windows();
    if (windowManager != null) {
        var modalWindow = windowManager[index];
        //Set width, height of modal window
        var scrW = getScreenWidth();
        var scrH = getScreenHeight();
        var sizeRate = 0.95;
        if (index == 2)
            sizeRate = 0.9;
        else if (index == 3)
            sizeRate = 0.85;
        else if (index == 4)
            sizeRate = 0.8;
        if (width == null)
            width = scrW * sizeRate;
        if (height == null)
            height = scrH * sizeRate;
        modalWindow.setSize(width, height);
        //Set position(x,y) of modal window
        if (posX == null && posY == null) {
            posX = (scrW - width) / 2 + Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);
            posY = (scrH - height) / 2 + Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
        }
        modalWindow.MoveTo(posX, posX);
        
        modalWindow.setUrl(url);
        modalWindow.show();
    }
}

function CloseModalWindow(index) {
    if (index == null)
        GetRadWindow().Close();
    else {
        if (windowManagerID == '')
            windowManagerID = 'ctl00_ctl00_uxWindowManager';
        var windowManager = $find(windowManagerID);
        windowManager = windowManager.get_windows();
        if (windowManager != null)
            windowManager[index].close();
        else
            GetRadWindow().Close();
    }
}

function OnCloseModalWindow(sender, eventArgs) {
    var obj = sender.get_contentFrame().contentWindow;
    if (obj != null && obj.CloseModalWindowCallback != null) obj.CloseModalWindowCallback();
}

//*************************End Popup Modal*************************

//*************************Global Functions*************************

//Trim string
String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

function coreValidate(clientId,maxL,minL,onlyEx,exceptEx,formatEx,maxMsg,minMsg,invalidMsg,invalidFormat){
    var obj = document.getElementById(clientId);

    var ret = true;
    if(obj.disabled)
    return ret;
    var inputValue = obj.value;
    if(maxL!=null){
        if(inputValue.length>maxL){
            alert(maxMsg);
            ret = false;
        }
    }
    if(minL!=null&&ret){
        var tempValue = inputValue; //trim(inputValue);
        if(minL == 0 && tempValue.length == 0) return true;
        if(tempValue.length<minL){
            alert(minMsg);
            ret = false;
        }
    }
    if(onlyEx!=null&&ret){
        if(!hierarchyHasOnlyCharacters(inputValue,onlyEx)){
            alert(invalidMsg);
            ret = false;
        }
    }
    
    if(exceptEx!=null&&ret){
        if(hierarchyHasInvalidCharacters(inputValue,exceptEx)){
            alert(invalidMsg);
            ret = false;
        }
    }
    
    if(formatEx!=null&&ret){
        if(!hierarchyValidateFormat(inputValue,formatEx)){
            alert(invalidFormat);
            ret = false;
        }
    }
        
    if(!ret){ 
        obj.focus();
        obj.select();
    }
    return ret;
}

function coreHasInvalidCharacters(input,invalid_chars){
    if(invalid_chars==null) return true;
    if(invalid_chars=='') return true;
    var invalidHexValue = '';
    for(var i=0;i<invalid_chars.length;i++){
        invalidHexValue += '\\x'+invalid_chars.charCodeAt(i).toString(16).toUpperCase();
    }
    return input.search(new RegExp('['+invalidHexValue+']','ig')) >= 0;
}

function coreHasOnlyCharacters(input,valid_chars){
    if(valid_chars==null) return true;
    if(valid_chars=='') return true;
    var validHexValue = '';
    for(var i=0;i<valid_chars.length;i++){
        validHexValue += '\\x'+valid_chars.charCodeAt(i).toString(16).toUpperCase();
    }
    var m = input.match(new RegExp('['+validHexValue+']','ig'));
    if(m!=null){
        return m.length == input.length;
    }
    return false;
}

function coreValidateFormat(input,validHexValue){
    if(validHexValue==null) return true;
    if(validHexValue=='') return true;
    var m = input.match(new RegExp(validHexValue,'ig'));
    if(m!=null){
        return m == input;
    }
    return false;
}
//*************************Global Functions*************************

